# Build recipe for qi.
#
# Copyright (c) 2017-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=qi
version=2.9-rc1
arch=noarch
release=1

# Define a category for the output of the package name
pkgcategory=tools

tarname=${program}-${version}.tar.lz

# Remote source(s)
fetch="
 https://dragora.mirror.garr.it/current/sources/$tarname
 rsync://rsync.dragora.org/current/sources/$tarname
"

description="
A user-friendly package manager (version $version).

Qi is a simple but well-integrated package manager.  It can create,
install, remove, and upgrade software packages.  Qi produces binary
packages using recipes, which are files containing specific instructions
to build each package from source.  Qi can manage multiple packages
under a single directory hierarchy.  This method allows to maintain a
set of packages and multiple versions of them.  This means that Qi could
be used as the main package manager or complement the existing one.

Qi offers a friendly command line interface, a global configuration
file, a simple recipe layout to deploy software packages; also works
with binary packages in parallel, speeding up installations and packages
in production.  The format used for packages is a simplified and safer
variant of POSIX pax archive compressed in lzip format.

Qi is a modern (POSIX-compliant) shell script released under the
terms of the GNU General Public License.  There are only two major
dependencies for the magic: graft(1) and tarlz(1), the rest is expected
to be found in any Unix-like system.
"

homepage=https://www.dragora.org
license=GPLv3+

# Source documentation
docs="AUTHORS COPYING CREDITS NEWS README.md doc/example.order doc/recipe*"
docsdir="${docdir}/${program}"

# Limit package name to the program name
full_pkgname="${program}@${pkgcategory}"

build()
{
    # Figure out architecture name for default package names in Dragora 3+
    #
    # See git_tree/targets/ for 'package_arch' on it.
    #
    case $(gcc -dumpmachine) in
    aarch64-*)
        package_arch=arm64
        ;;
    arm-*-musleabi)
        package_arch=armfp
        ;;
    arm-*-musleabihf)
        package_arch=armhf
        ;;
    armv7hl-*-musleabihf)
        package_arch=armhl
        ;;
    *x32-*)
        package_arch=x32
        ;;
    x86_64-*)
        package_arch=amd64
        ;;
    powerpc64le-*)
        package_arch=ppc64le
        ;;
    esac

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure \
     --prefix=/usr \
     --libexecdir=/usr/libexec \
     --bindir=/usr/bin \
     --sbindir=/usr/sbin \
     --sysconfdir=/etc \
     --localstatedir=/var \
     --infodir=/usr/share/info \
     --mandir=/usr/share/man \
     --docdir=/usr/share/doc \
     --packagedir=/usr/pkg \
     --targetdir=/ \
     --arch="${package_arch:-$(uname -m)}"

    unset -v package_arch
    make -j${jobs} DESTDIR="$destdir" install

    ln -s qi "${destdir}/usr/bin/dragora-qi"

    # Copy the config file used in the temporary system
    if test -r /tools/etc/qirc && test ! -e /etc/qirc
    then
        cp -p /tools/etc/qirc "${destdir}/etc/"
        cp -p /tools/etc/qirc "${destdir}/etc/qirc.bak"
        chmod 644 "${destdir}"/etc/qirc*
    fi

    # Manage dot new file(s)
    touch "${destdir}/etc/.graft-config"

    # Compress and copy source documents

    rm -f "${destdir}/${infodir}/dir"
    lzip -9 "${destdir}/${infodir}/qi.info" \
            "${destdir}/${mandir}/man1/qi.1"

    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

